﻿using PropertyChanged;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VIA.Integration;
using VIA.SiMoJi.Domain.Electronics;

namespace VIA.SiMoJi.Presentation.ViewModel
{
    [AddINotifyPropertyChangedInterface]
    public class NozzleDebugViewModel
    {
        public Nozzle Nozzle { get; set; }
        public string Name { get; set; }
        public bool IsSucked { get; set; }
        public bool IsBroken { get; set; }
        public bool IsStoped { get; set; }
        public bool IsSensorOn { get; set; }
        public string SensorDI { get; set; }
        public NozzleDebugViewModel(Nozzle nozzle)
        {
            Nozzle = nozzle;
            Name = Nozzle?.Name;
            if (nozzle?.Sensor is CardIoPoint pt)
            {
                SensorDI = $"DI.{pt.PortNo}";
                IsSucked = nozzle.IsOn();
            }
        }

        public async Task<bool> Suck()
        {
            if (IsSucked)
            {
                return await Stop();
            }
            await Nozzle.SuckAndCheckAsync();
            IsSucked = true;
            IsBroken = false;
            IsStoped = false;
            IsSensorOn = Nozzle.IsOn();
            return IsSensorOn;
        }

        public async Task<bool> Broke()
        {
            if (IsBroken)
            {
                return await Stop();
            }
            await Nozzle.BrokenAndCheckAsync();
            IsSucked = false;
            IsBroken = true;
            IsStoped = false;
            IsSensorOn = Nozzle.IsOn();
            return IsSensorOn;
        }

        public async Task<bool> Stop()
        {
            Nozzle.StopAsync();
            await Task.Delay(300);
            IsSucked = false;
            IsBroken = false;
            IsStoped = true;
            IsSensorOn = Nozzle.IsOn();
            return IsSensorOn;
        }
    }
}
